package cn.edu.nju.ws.sview.crawler;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.rdf.URIResource;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.index.SearchIndexManager;

import com.hp.hpl.jena.ontology.OntModel;

/**
 * 任务执行进程
 * @author Gong Saisai
 *
 */
public class TaskThread implements Runnable{
//	 static Logger logger = LogUtil.getLogger(TaskThread.class.getName()) ;
	 private LinkedList<URIResource> pool = null;
	 private  HashSet<URIResource> done = null;
 	 private  HashSet<URIResource> busy = null;
 	 private CrawlerMeta meta = null;
 	 private Object lockOfURIStatus = null;
 	 public TaskThread(LinkedList<URIResource> pool, HashSet<URIResource> done,HashSet<URIResource> busy,Object lockOfURIStatus,CrawlerMeta meta){
 		 this.pool = pool;
 		 this.done = done;
		 this.busy = busy;
		 this.meta = meta;
		 this.lockOfURIStatus =lockOfURIStatus;
 	 }
	@Override
	public void run() {
		// TODO Auto-generated method stub
        while(true){
        	URIResource uriResource = null;
			boolean waitAndContinue = false;
			synchronized (this.pool) {
				if (this.pool.isEmpty()) {
					waitAndContinue = true;
				}else{
					uriResource = this.pool.poll();
				}
			}
			if(waitAndContinue){
				try {
					Thread.sleep(10) ;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				continue;
			}
			if(uriResource ==null){
				break;
			}
			
			//获取权威文档URI
			URIResource dereferenceDoc = null;
			Fetch fetch = new Fetch(uriResource.getURI(),this.meta);
			dereferenceDoc = fetch.getDereferencDocument();
			//check 权威文档内容，如果符合要求，则进行存储三元组以及文档dereferencInfo；否则则删除旧版本
			//无论成功与失败都要记录文件尝试下载的记录。
			long timestamp = System.currentTimeMillis();
			if (dereferenceDoc != null) {
				OntModel model = fetch.checkContent(dereferenceDoc);
				Store store = new Store();
				try{
				if (model != null) {
					// 更新新的三元组
					store.updateRDFDocument(dereferenceDoc, model, timestamp) ;
					//设置dereference document信息
					store.setDereferenceInfo(dereferenceDoc.getURI(), dereferenceDoc.getURI());
					if(!uriResource.equals(dereferenceDoc)){
						store.setDereferenceInfo(uriResource.getURI(), dereferenceDoc.getURI());
						store.deleteRDFDocument(uriResource) ;
					}
					model.close();
				} else {
					// 删除旧版本三元组
					store.deleteRDFDocument(dereferenceDoc) ;
				}
				}catch(Exception e){
					e.printStackTrace();
				}
				fetch.close();
				store.reset();
				
				/*for dataset browsing*/
				ArrayList<DatasetItem> dss = new ArrayList<DatasetItem>();
				int ds_id =-1;
				try {
					ds_id =URIIDCache.getInstance().getURIID( dereferenceDoc.getURI(), true);
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dss.add(new DatasetItem("uri", ds_id, dereferenceDoc.getURI()));
//				IndexManager.getIndexManager().addDatasets(dss);
				SearchIndexManager.getIndexManager().addDatasets(dss);
			}
			//虽然可能尚未取得权威文档，但是依然记录ping时间
			Store.setLastPingTime(uriResource.getURI(), timestamp) ;
			synchronized(this.lockOfURIStatus){
				this.busy.remove(uriResource);
				this.done.add(uriResource);
			}
		}
	}

}
